package com.example.administrator.yunniuusedcar.activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.example.administrator.common.base.BaseActivity;
import com.example.administrator.yunniuusedcar.fragment.AgeFragment;
import com.example.administrator.yunniuusedcar.fragment.DefaultFrament;
import com.example.administrator.yunniuusedcar.fragment.KilometreNumberFrament;
import com.example.administrator.yunniuusedcar.fragment.PriceFragment;
import com.example.administrator.yunniuusedcar.fragment.ScreenFragment;
import com.example.administrator.yunniuusedcar.R;

/**
 *  首页 --> 二手豪车 （RadioGroup + fragment ）
 */

public class SeconHandCarActivity extends BaseActivity {

    Fragment[] fragments ;
    private RadioGroup mRgBottomMenu ;
    //当前fragment的下标
    private int currentIndex ;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_secon_hand_car;
    }

    @Override
    public void initView() {
        mRgBottomMenu  = findView(R.id.rg_bottom_menu);    // RadioGroup
        initFg();
      ImageView iv_bacds = findView(R.id.iv_back);
        iv_bacds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initFg() {
        fragments = new Fragment[]{   // 讲fragment加入数组
                // 默认，公里数，年龄，价格，筛选
                new DefaultFrament(), new KilometreNumberFrament(),
                new AgeFragment(), new PriceFragment(), new ScreenFragment()
        };
        // 开启事物
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //设置为默认界面 MainHomeFragment
        ft.add(R.id.main_content,fragments[0]).commit();

        //RadioGroup选中事件监听 改变fragment  RadioGroup
        mRgBottomMenu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_default :  // 默认
                        setIndexSelected(0);
                        break;
                    case R.id.rb_KLnum:   // 公里数
                        setIndexSelected(1);
                        break;
                    case R.id.rb_age :  // 年龄
                        setIndexSelected(2);
                        break;
                    case R.id.rb_money:   // 价格
                        setIndexSelected(3);
                        break;
                    case R.id.rb_person :  // 筛选
                        setIndexSelected(4);
                        break;
                }
            }
        });
    }

    // 设置fragment 界面
    private void setIndexSelected(int index) {
        if (currentIndex == index){
            return;
        }
        //开启事务
        FragmentTransaction ft1 = getSupportFragmentManager().beginTransaction();
        //隐藏当前Fragment
        ft1.hide(fragments[currentIndex]);
        // 判断当前的fragmetn是否已添加
        if (!fragments[index].isAdded()){
            ft1.add(R.id.main_content,fragments[index]).show(fragments[index]);
//            ft1.add(R.id.main_content,fragments[index].show(fragments[index]));
        }else {
            //否则显示新 fragment
            ft1.show(fragments[index]);

        }ft1.commit();
        currentIndex = index ;

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }
}
